package cn.com.surker.mes.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.mes.dao.MesTransportPriceDao;
import cn.com.surker.mes.entity.MesTransportPrice;
import cn.com.surker.mes.service.MesTransportPriceBaseService;
import cn.com.surker.util.DateUtil;
import cn.com.surker.vo.mes.MesTransportPriceVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Transactional
public abstract class MesTransportPriceBaseBaseServiceImpl extends ServiceImpl<MesTransportPriceDao, MesTransportPrice> implements MesTransportPriceBaseService {

    @Override
    @Transactional(readOnly = true)
    public Map<String, List<MesTransportPriceVO>> findByContract(Long contractId) {
        Map<String, List<MesTransportPriceVO>> map = new HashMap<>();
        QueryWrapper<MesTransportPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MesTransportPrice::getContractId, contractId);
        List<MesTransportPrice> listMes = baseMapper.selectList(queryWrapper);
        if(CollUtil.isNotEmpty(listMes)){
            List<MesTransportPriceVO> listVo = new ArrayList<>();;
            for (MesTransportPrice price : listMes){
                MesTransportPriceVO mesTransportPriceVO = BeanUtil.copyProperties(price, MesTransportPriceVO.class);
                String takeTime = DateUtil.convertString(mesTransportPriceVO.getTakeTime());
                mesTransportPriceVO.setGroupTime(takeTime);
                listVo.add(mesTransportPriceVO);
            }
            map = listVo.stream().collect(Collectors.groupingBy(MesTransportPriceVO::getGroupTime));
        }
        return map;
    }

    @Override
    public Boolean savePrice(MesTransportPrice price) {
        List<MesTransportPrice> listTask = getMesListByContractIdAndTakeTime(price);
        if(CollUtil.isNotEmpty(listTask)){
            throw new MessageCenterException("该配置已经存在相同的数据,请重新修改配置！");
        }
        return save(price);
    }

    @Override
    public Boolean updatePrice(MesTransportPrice price) {
        MesTransportPrice mesTransportPrice = getMesTransportPrice(price.getId());
        price.setContractId(mesTransportPrice.getContractId());
        List<MesTransportPrice> listTask = getMesListByContractIdAndTakeTime(price);
        if(CollUtil.isNotEmpty(listTask)){
            throw new MessageCenterException("该配置已经存在相同的数据,请重新修改配置！");
        }
        BeanUtil.copyProperties(price, mesTransportPrice);
        mesTransportPrice.setUpdateTime(new Date());
        return updateById(mesTransportPrice);
    }

    @Override
    public Boolean updateStatus(MesTransportPrice price) {
        MesTransportPrice mesTransportPrice = getMesTransportPrice(price.getId());
        if(mesTransportPrice.getStatus().intValue() == price.getStatus().intValue()){
            throw new MessageCenterException("当前状态未变化，不需要更改！");
        }
        BeanUtil.copyProperties(price, mesTransportPrice);
        mesTransportPrice.setUpdateTime(new Date());
        return updateById(mesTransportPrice);
    }

    public MesTransportPrice getMesTransportPrice(Long id) {
        MesTransportPrice mesTransportPrice = baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(mesTransportPrice)){
            throw new MessageCenterException("PC物料运输单价数据不存在！");
        }
        return mesTransportPrice;
    }
    public List<MesTransportPrice> getMesListByContractIdAndTakeTime(MesTransportPrice price){
        QueryWrapper<MesTransportPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MesTransportPrice::getContractId, price.getContractId());
        queryWrapper.lambda().eq(MesTransportPrice::getTakeTime, price.getTakeTime());
        queryWrapper.lambda().eq(MesTransportPrice::getCountType, price.getCountType());
        queryWrapper.lambda().eq(MesTransportPrice::getPriceType, price.getPriceType());
        queryWrapper.lambda().eq(MesTransportPrice::getCarModel, price.getCarModel());
        queryWrapper.lambda().eq(MesTransportPrice::getMaxDistance, price.getMaxDistance());
        queryWrapper.lambda().eq(MesTransportPrice::getPrice, price.getPrice());
        if(ObjectUtil.isNotEmpty(price.getId())){
             queryWrapper.lambda().ne(MesTransportPrice::getId, price.getId());
        }
        return baseMapper.selectList(queryWrapper);
    }

}
